package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.entity.Wallet;
import com.yc.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/wallet")
public class WalletController {
    @Autowired
    private WalletService walletService;

    /**
     * 查询钱包余额
     * @param userId
     * @return
     */
    @GetMapping("/getBalance/{userId}")
    public Wallet getBalance(@PathVariable("userId") Integer userId){
        QueryWrapper<Wallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Wallet wallet = walletService.getOne(queryWrapper);
        return wallet;
    }

    /**
     * 消费
     * @param userId
     * @param money
     * @return
     */
    @PutMapping("/spend/{userId}/{money}")
    public String spend(@PathVariable("userId") Integer userId,
                        @PathVariable("money") Double money){
        QueryWrapper<Wallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Wallet wallet = walletService.getOne(queryWrapper);
        int result = walletService.spend(wallet,money);
        return result>0?"付款成功":"付款失败";
    }

    /**
     * 退款
     * @param userId
     * @param money
     * @return
     */
    @PutMapping("/refund/{userId}/{money}")
    public String refund(@PathVariable Integer userId,
                         @PathVariable Double money){
        QueryWrapper<Wallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Wallet wallet = walletService.getOne(queryWrapper);
        int result = walletService.refund(wallet,money);
        return result>0?"退款成功":"退款失败";
    }
}

